package com.home.school.controller;

import com.home.school.bean.TUser;
import com.home.school.service.TUserService;
import com.home.school.vo.UserListVO;
import com.home.school.vo.UserModifyPwd;
import com.home.school.vo.UserModifyVO;
import com.home.school.vo.UserVO;
import com.home.school.model.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/api/user")
@RestController
public class UserController {

    @Autowired
    private TUserService tUserService;

    /**
     * 保存 用户
     * @param userVO
     * @return
     */
    @RequestMapping("/save")
    public R save(@RequestBody TUser userVO){
        tUserService.save(userVO);
        return R.ok();
    }

    /**
     * 注册
     * @param userVO
     * @return
     */
    @RequestMapping("/register")
    public R register(@RequestBody UserVO userVO){
        TUser tUser = userVO.transferToUser();
        tUserService.save(tUser);
        return R.ok();
    }
    /**
     *  修改密码
     * @param userVO
     * @return
     */
    @RequestMapping("/rePwd")
    public R resetPwd(@RequestBody UserModifyPwd userVO){
        tUserService.resetPwd(userVO);
        return R.ok();
    }

    /**
     * 登录
     * @param userVO
     * @return
     */
    @RequestMapping("login")
    public R login(@RequestBody UserVO userVO){
        return tUserService.login(userVO);
    }

    /**
     * 修改 用户
     * @param userVO
     * @return
     */
    @RequestMapping("modify")
    public R modify(@RequestBody UserModifyVO userVO){
        return tUserService.modify(userVO);
    }

    /**
     * id 查询 用户
     * @param id
     * @return
     */
    @RequestMapping("byId/{id}")
    public R byId(@PathVariable("id") String id){
        return R.ok().data(tUserService.getById(id));
    }

    /**
     * 删除 用户 id
     * @param id
     * @return
     */
    @RequestMapping("deleted/{id}")
    public R deleted(@PathVariable("id") Integer id){
        return R.ok().data(tUserService.removeById(id));
    }

    /**
     * 列表 查询
     * @param userListVO
     * @return
     */
    @RequestMapping("getList")
    public R getListUrl(@RequestBody UserListVO userListVO){
        return tUserService.pageList(userListVO);
    }
}
